﻿#include "StudyQtWebEngine.h"

StudyQtWebEngine::StudyQtWebEngine(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    QVBoxLayout* pLayoutMain = new QVBoxLayout(ui.centralWidget);
    ui.centralWidget->setLayout(pLayoutMain);
    pLayoutMain->setContentsMargins(0, 30, 0, 0);
    {
        QWebEngineView* pView = new QWebEngineView(ui.centralWidget);
        pLayoutMain->addWidget(pView);
        {
            m_pWebEnginePage = new AMWebEnginePage(pView);
            pView->setPage(m_pWebEnginePage);
            connect(m_pWebEnginePage, &AMWebEnginePage::sgnDownloadFileFinished, this, &StudyQtWebEngine::sltDownloadFileFinished);
        }

        QWebEngineView* view1 = new QWebEngineView(pView);
        pLayoutMain->addWidget(view1);
        pView->page()->setDevToolsPage(view1->page());

        pView->load(QUrl("https://www.baidu.com"));
    }

    connect(ui.btnDownload, &QPushButton::clicked, this, &StudyQtWebEngine::TestBtnDownloadClicked);

    this->showMaximized();
}

void StudyQtWebEngine::TestBtnDownloadClicked()
{
    QUrl url = "https://b.bdstatic.com/searchbox/icms/searchbox/img/cheng_girl.png";
    QString suggestedFileName = "123456789.png";
    m_pWebEnginePage->download(url, suggestedFileName);
}

void StudyQtWebEngine::sltDownloadFileFinished(QWebEngineDownloadItem* download)
{
    QString strTiShi = download->suggestedFileName() + TR("下载完成");
    QString strNeiRong = TR("文件目录为：") + download->downloadDirectory() + TR("\n");
    strNeiRong += TR("文件名为：") + download->downloadFileName() + TR("\n");
    strNeiRong += TR("下载中断编码为：") + QString::number((int)download->interruptReason()) + TR("\n");
    strNeiRong += TR("下载中断原因为：") + download->interruptReasonString() + TR("\n");
    QMessageBox::information(this, strTiShi, strNeiRong);
}
